package com.lsy.onehot.rnn.main;

import org.datavec.api.writable.Text;
import org.datavec.api.writable.Writable;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;

import com.gg.lsy.ai.uitls.ini.ZztPropKit;
import com.lsy.onehot.rnn.qa.CurpusProcess;
import com.lsy.onehot.rnn.qa.MultiLayerNetworkOneHotNN;
import com.lsy.onehot.rnn.qa.OneHotProcess;
import com.lsy.onehot.rnn.qa.MultiLayerNetworkMateVector;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * @author Scruel Tao
 */
public class DeeplearingOneHotCore {
	private MultiLayerNetwork model;
	private MultiLayerNetworkMateVector sent2Vec;
	private int numInputs;
	public  Map<Long, String> dict_A = new HashMap<>();
	private boolean isLoad =  true;
	
	public static void main(String[] args) throws Exception {
		System.out.println("请稍后，正在初始化(学习平台)...");
		ZztPropKit.use(new File("oneshot/settings.properties") );

		DeeplearingOneHotCore campusQA = new DeeplearingOneHotCore();
		campusQA.run();
		// 初始化完毕
		System.out.println("你好(请继续输入，和我对话)");
		Scanner input = new Scanner(System.in);
		String text;
		while (true) {
			text = input.nextLine();
			if (text.contains("再见"))
				return;
			System.out.println(campusQA.getAnswer(text));
		}
	}
	

	public void setLoad(boolean isLoad){
		this.isLoad = isLoad ;
	}
	
	public void run() throws Exception{
		CurpusProcess cur = new CurpusProcess();
		cur.handleSigalDict(); 
		cur.handleDict() ;
		OneHotProcess onehot = new OneHotProcess();
		onehot.setICurpusMateSet(cur);
		// 字典向量大小
		this.numInputs = cur.getInputNums() ;
		MultiLayerNetworkOneHotNN oneHotNN = new MultiLayerNetworkOneHotNN();
		oneHotNN.setIOneHotProcess(onehot);
		if(isLoad){
			this.model = oneHotNN.restoreComputationGraph() ;
		}else
		{
			oneHotNN.initialModel(this.numInputs);
			this.model = oneHotNN.getModel();
		}
		
		this.sent2Vec = new MultiLayerNetworkMateVector();
		sent2Vec.setICurpusMateSet(cur);
		
		dict_A =  cur.getDict_A() ;
	}
	
	public DeeplearingOneHotCore()  {
	
	}

	
	public String getAnswer(String text) {
		String val = sent2Vec.getMatrixString(text);
//		System.out.println("varl:"+ val );
		String[] split = val.split(",", -1);
		List<Writable> ret = new ArrayList<Writable>();
		for (String s : split) {
			ret.add(new Text(s));
		}
		// 获取结果集
		INDArray featureVector = Nd4j.create(numInputs);
		int featureCount = 0;
		for (int j = 0; j < ret.size(); j++) {
			Writable current = ret.get(j);
			double value = current.toDouble();
			featureVector.putScalar(featureCount++, value);
		}
		 System.out.println(featureVector);
		INDArray predicted = model.output(featureVector, false);
		INDArray binaryGuesses = predicted.gt(0.05);
		 System.out.println(predicted.maxNumber().doubleValue());
		 System.out.println(featureVector.maxNumber().doubleValue());
		 
		if (binaryGuesses.maxNumber().doubleValue() == 1) {
			for (int i = 0; i < dict_A.size(); i++) {
				if (binaryGuesses.getDouble(i) == 1) {
					System.out.println("get:"+ i );
					long key = i;
					return dict_A.get(key);
				}
			}
			return "Index Error";
		} else if (featureVector.maxNumber().doubleValue() != 0) {
			return "\"" + text + "\"描述的不是很具体，请具体描述一下~";
		} else
			return "抱歉，暂未收录哦~";
	}

}
